/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseUserRoleDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseUserRoleDao : BaseDao
    {
        public BaseUserRoleDao()
        {
            base.CurrentTableName = BaseUserRoleTable.TableName;
        }

        public BaseUserRoleDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseUserRoleDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseUserRoleDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseUserRoleEntity userRoleEntity)
        {
            return this.AddEntity(userRoleEntity);
        }

        private string AddEntity(BaseUserRoleEntity userRoleEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseUserRoleTable.TableName);
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BaseUserRoleTable.TableName);
            sqlBuilder.SetValue(BaseUserRoleTable.FieldID, sequence);
            this.SetEntity(sqlBuilder, userRoleEntity);
            sqlBuilder.SetValue(BaseUserRoleTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseUserRoleTable.FieldCreateDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return sequence;
        }

        public int AddToRole(string[] userIDs, string[] roleIDs)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseUserRoleTable.TableName, userIDs.Length * roleIDs.Length);
            for (int i = 0; i < userIDs.Length; i++)
            {
                for (int j = 0; j < roleIDs.Length; j++)
                {
                    this.AddToRole(strArray[(i * roleIDs.Length) + j], userIDs[i], roleIDs[j]);
                    num++;
                }
            }
            return num;
        }

        public int AddToRole(string[] userIDs, string roleID)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseUserRoleTable.TableName, userIDs.Length);
            for (int i = 0; i < userIDs.Length; i++)
            {
                this.AddToRole(strArray[i], userIDs[i], roleID);
                num++;
            }
            return num;
        }

        public string AddToRole(string userID, string roleID)
        {
            return this.AddToRole(string.Empty, userID, roleID);
        }

        public int AddToRole(string userID, string[] roleIDs)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseUserRoleTable.TableName, roleIDs.Length);
            for (int i = 0; i < roleIDs.Length; i++)
            {
                this.AddToRole(strArray[i], userID, roleIDs[i]);
                num++;
            }
            return num;
        }

        private string AddToRole(string pramID, string userID, string roleID)
        {
            BaseUserRoleEntity userRoleEntity = new BaseUserRoleEntity
            {
                ID = pramID,
                UserID = userID,
                RoleID = roleID
            };
            return this.Add(userRoleEntity);
        }

        public string GetRoleFullName(string id)
        {
            string property = this.GetProperty(id, BaseUserRoleTable.FieldRoleID);
            return DbLogic.GetProperty(base.DbHelper, BaseRoleTable.TableName, BaseRoleTable.FieldID, property, BaseRoleTable.FieldRealname);
        }

        public string[] GetRoleIDs(string userID)
        {
            string[] names = new string[1];
            string[] values = new string[1];
            names[0] = BaseUserRoleTable.FieldUserID;
            values[0] = userID;
            return BaseBusinessLogic.FieldToArray(this.GetDT(names, values), BaseUserRoleTable.FieldRoleID);
        }

        public string GetUserFullName(string id)
        {
            string property = this.GetProperty(id, BaseUserRoleTable.FieldUserID);
            return DbLogic.GetProperty(base.DbHelper, BaseStaffTable.TableName, BaseStaffTable.FieldID, property, BaseStaffTable.FieldRealname);
        }

        public string[] GetUserIDs(string roleID)
        {
            string[] names = new string[1];
            string[] values = new string[1];
            names[0] = BaseUserRoleTable.FieldRoleID;
            values[0] = roleID;
            return BaseBusinessLogic.FieldToArray(this.GetDT(names, values), BaseUserRoleTable.FieldUserID);
        }

        public int RemoveFormRole(string[] userIDs, string[] roleIDs)
        {
            int num = 0;
            for (int i = 0; i < userIDs.Length; i++)
            {
                for (int j = 0; j < roleIDs.Length; j++)
                {
                    num += this.RemoveFormRole(userIDs[i], roleIDs[j]);
                }
            }
            return num;
        }

        public int RemoveFormRole(string userID, string roleID)
        {
            string[] names = new string[2];
            string[] strArray2 = new string[2];
            names[0] = BaseUserRoleTable.FieldUserID;
            strArray2[0] = userID;
            names[1] = BaseUserRoleTable.FieldRoleID;
            strArray2[1] = roleID;
            return this.BatchDelete(names, (object[])strArray2);
        }

        public int RemoveFormRole(string userID, string[] roleIDs)
        {
            int num = 0;
            for (int i = 0; i < roleIDs.Length; i++)
            {
                num += this.RemoveFormRole(userID, roleIDs[i]);
            }
            return num;
        }

        public int RemoveFormRole(string[] userIDs, string roleID)
        {
            int num = 0;
            for (int i = 0; i < userIDs.Length; i++)
            {
                num += this.RemoveFormRole(userIDs[i], roleID);
            }
            return num;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseUserRoleEntity userRoleEntity)
        {
            sqlBuilder.SetValue(BaseUserRoleTable.FieldUserID, userRoleEntity.UserID);
            sqlBuilder.SetValue(BaseUserRoleTable.FieldRoleID, userRoleEntity.RoleID);
            sqlBuilder.SetValue(BaseUserRoleTable.FieldDescription, userRoleEntity.Description);
            sqlBuilder.SetValue(BaseUserRoleTable.FieldEnabled, userRoleEntity.Enabled ? 1 : 0);
        }
    }
}